package com.company.test16;

import com.company.Snack.*;
import com.company.Snack.Point;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * Created by Administrator on 2016.9.6.
 */
public class Test1628 extends JFrame {
    public Test1628(){
        add(new Stroll());
    }

    public static void main(String args[]){
        JFrame frame=new Test1628();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(200,200);
        frame.setTitle("自随机漫步");
        frame.setVisible(true);
        frame.setSize(250,250);
    }

    class Stroll extends JPanel{
        final int DOWN=1;
        final int UP=0;
        final int LEFT=2;
        final int RIGHT=3;

        int state;

        Point now;

        ArrayList<Point> arrayList;

        public Stroll(){
            arrayList=new ArrayList<>();
            now=new Point(100,100);

            state=(int)(Math.random()*4);
            Timer timer=new Timer(100,new TimerListener());
            timer.start();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            g.setColor(Color.PINK);

            for (int i=0;i<21;i++){
                g.drawLine(i*10,0,i*10,200);
            }

            for (int i=0;i<21;i++){
                g.drawLine(0,i*10,200,i*10);
            }

            g.setColor(Color.BLACK);

            for (int i=0;i<arrayList.size()-1;i++){
                g.drawLine(arrayList.get(i).getX(),arrayList.get(i).getY(),
                        arrayList.get(i+1).getX(),arrayList.get(i+1).getY());
            }
        }

        class TimerListener implements ActionListener{
            @Override
            public void actionPerformed(ActionEvent e) {
                if(isEnd()||isBreak()){
                    return;
                }

                int sign=(int)(Math.random()*4);
                while (!isPass(sign)){
                    sign=(int)(Math.random()*4);
                }

                go(sign);

                repaint();
            }
        }

        //是否已经被三面围着
        public boolean isEnd(){
            int z=0;
            if(this.isPass(UP)){
                z=1;
            }else if(this.isPass(DOWN)){
                z=1;
            }else if(this.isPass(LEFT)){
                z=1;
            }else if(this.isPass(RIGHT)){
                z=1;
            }

            return z==0;
        }

        //是否向某个方向可进
        public boolean isPass(int sign){
            Point now=this.advance(sign);

            for(Point point:arrayList){
                if(point.equals(now)){
                    return false;
                }
            }

            return true;
        }

        //判断是否已经越界
        public boolean isBreak(){
            if(now.getX()>=200||now.getX()<=0){
                return true;
            }

            if(now.getY()>=200||now.getY()<=0){
                return true;
            }

            return false;
        }

        //向前走一步若成功返回true
        public boolean go(int sign){
            Point now=this.advance(sign);

            for(Point point:arrayList){
                if(point.equals(now)){
                    return false;
                }
            }
            this.now=now;
            arrayList.add(this.now);
            state=sign;
            return true;
        }

        //给出一个点及前进方向，返回前进点
        public Point advance(int sign){
            Point now=this.now;

            switch (sign){
                case UP:now=new Point(this.now.getX(),this.now.getY()-10);break;
                case DOWN:now=new Point(this.now.getX(),this.now.getY()+10);break;
                case LEFT:now=new Point(this.now.getX()-10,this.now.getY());break;
                case RIGHT:now=new Point(this.now.getX()+10,this.now.getY());break;
            }
            return now;
        }
    }
}
